import { createRouter, createWebHistory } from 'vue-router';
import { useAuthStore } from '../stores/auth';
import Explore from '../views/Explore.vue';
import Login from '../views/Login.vue';
import Register from '../views/Register.vue';
import NoteDetail from '../views/NoteDetail.vue';
import Notifications from '../views/Notifications.vue';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', redirect: '/explore' },
    { path: '/explore', component: Explore },
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    { path: '/notes/:id', component: NoteDetail },
    { path: '/publish', component: () => import('../views/Publish.vue'), meta: { requiresAuth: true } },
    { path: '/notifications', component: Notifications, meta: { requiresAuth: true } },
  ],
});

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();
  if (to.meta.requiresAuth && !authStore.token) {
    return next(`/login?redirect=${to.path}`);
  }
  next();
});

export default router;